﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IBatisNet.DataMapper;
using StateGrid.OperationLog;

namespace StateGrid.NYBG
{
    /// <summary>
    /// Summary description for NYBG_DAO
    /// </summary>
    public class NYBG_DAO
    {
        /// <summary>
        /// 数据库操作对象
        /// </summary>
        private ISqlMapper sqlMapper = Mapper.Instance();

        /// <summary>
        /// 日志操作对象
        /// </summary>
        private OperationLog_DAO operationLogDAO = new OperationLog_DAO();


        /// <summary>
        /// 获取所有考察报告分类
        /// </summary>
        /// <returns></returns>
        public IList<NYBG_FL> GetAllNYBG_FL()
        {
            return sqlMapper.QueryForList<NYBG_FL>("SelectAllNYBG_FL", null);
        }

        /// <summary>
        /// 获取所有考察报告篇名信息
        /// </summary>
        /// <returns></returns>
        public IList<NYBG_PM> GetAllNYBG_PM()
        {
            return sqlMapper.QueryForList<NYBG_PM>("SelectAllNYBG_PM", null);
        }

        /// <summary>
        /// 按分类编码查找分类信息
        /// </summary>
        /// <param name="flbm"></param>
        /// <returns></returns>
        public NYBG_FL GetNYBG_FLByFlbm(String flbm)
        {
            return sqlMapper.QueryForObject<NYBG_FL>("SelectNYBG_FLByFlbm", flbm);
        }

        /// <summary>
        /// 按中文名称查找分类信息
        /// </summary>
        /// <param name="zwmc">中文名称</param>
        /// <returns></returns>
        public NYBG_FL GetNYBG_FLByZwmc(String zwmc)
        {
            return sqlMapper.QueryForObject<NYBG_FL>("SelectNYBG_FLByZwmc", zwmc);
        }

        /// <summary>
        /// 获取指定分类编码、年份的篇名数量
        /// </summary>
        /// <param name="sybm">分类编码</param>
        /// <param name="nf">年份</param>
        /// <returns></returns>
        public int GetCountSomeNYBG_PM(String flbm, String nf)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String wjm = null;

            if (null == flbm || "".Equals(flbm))
            {
            }
            else
            {
                wjm = flbm;
                if (null != nf && nf.Length == 4)
                {
                    wjm += '-' + nf;
                }
            }

            if (null != wjm)
            {
                wjm += '%';
            }

            para.Add("wjm", wjm);
            return sqlMapper.QueryForObject<int>("CountSomeNYBG_PM", para);
        }


        /// <summary>
        /// 获取指定索引编码、年份的篇名数量
        /// </summary>
        /// <param name="sybm">索引编码</param>
        /// <param name="nf">年份</param>
        /// <param name="start">起始位置</param>
        /// <param name="end">结束位置</param>
        /// <returns></returns>
        public IList<NYBG_PM> GetSomeNYBG_PM(String sybm, String nf,  int start, int end)
        {
            IDictionary<String, Object> para = new Dictionary<String, Object>();

            String wjm = null;

            if (null == sybm || "".Equals(sybm))
            {
            }
            else
            {
                wjm = sybm;
                if (null != nf && nf.Length == 4)
                {
                    wjm += '-' + nf;
                }
            }

            if (null != wjm)
            {
                wjm += '%';
            }

            para.Add("wjm", wjm);
            para.Add("start", start);
            para.Add("end", end);
            return sqlMapper.QueryForList<NYBG_PM>("SelectSomeNYBG_PM", para);
        }

        /// <summary>
        /// 获取指定文件名的篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <returns></returns>
        public NYBG_PM GetNYBG_PMByWjm(String wjm)
        {
            return sqlMapper.QueryForObject<NYBG_PM>("SelectNYBG_PMByWjm", wjm);
        }

        /// <summary>
        /// 插入一条分类信息
        /// </summary>
        /// <param name="NYBG_pm">分类信息</param>
        /// <returns></returns>
        public Boolean InsertNYBG_FL(NYBG_FL NYBG_fl)
        {
            Boolean flag = true;
            try
            {
                sqlMapper.Insert("InsertNYBG_FL", NYBG_fl);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 插入一条篇名信息
        /// </summary>
        /// <param name="NYBG_pm">篇名信息</param>
        /// <returns></returns>
        public Boolean InsertNYBG_PM(NYBG_PM NYBG_pm)
        {
            Boolean flag = true;
            OperationLogItem operationLog = this.GetOperationLogItem(NYBG_pm, "INSERT", null);
            try
            {
                sqlMapper.Insert("InsertNYBG_PM", NYBG_pm);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 更新一条分类信息
        /// </summary>
        /// <param name="nybg_fl">要更新的分类信息</param>
        /// <returns></returns>
        public Boolean UpdateNYBG_FL(NYBG_FL nybg_fl)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateNYBG_FL", nybg_fl);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;

        }
        /// <summary>
        /// 更新一条篇名信息
        /// </summary>
        /// <param name="nybg_pm">要更新的篇名信息</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>
        public Boolean UpdateNYBG_PM(NYBG_PM nybg_pm, String uname)
        {
            Boolean flag = true;
            int back = sqlMapper.Update("UpdateNYBG_PM", nybg_pm);
            if (back <= 0)
            {
                flag = false;
            }
            if (flag)
            {
                OperationLogItem operationLog = this.GetOperationLogItem(nybg_pm, "UPDATE", uname);
                this.operationLogDAO.insertLogItem(operationLog);
            }
            return flag;
        }

        /// <summary>
        /// 按文件名删除篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <returns></returns>
        public Boolean DeleteNYBG_FL(String flbm)
        {
            Boolean flag = true;
            int back = sqlMapper.Delete("DeleteNYBG_FLByFlbm", flbm);
            if (back <= 0)
            {
                flag = false;
            }
            return flag;
        }

        /// <summary>
        /// 按文件名删除篇名信息
        /// </summary>
        /// <param name="wjm">文件名</param>
        /// <param name="uname">操作用户名</param>
        /// <returns></returns>

        public int DeleteNYBG_PM(String wjm, String uname)
        {
            int result = 0;

            NYBG_PM NYBGPM = this.GetNYBG_PMByWjm(wjm);

            OperationLogItem operationLog = null;


            //切分文件名
            String[] str = wjm.Split('-');
            if (str.Length < 4)
            {
                return result;
            }
            //文件名前缀
            String wjmStr = str[0] + "-" + str[1] + "-";

            //删除指定篇名及文件
            if (OperationFile.DeleteFile(wjm))
            {
                result = sqlMapper.Delete("DeleteNYBG_PMByWjm", wjm);
                operationLog = this.GetOperationLogItem(NYBGPM, "DELETE", uname);
                if (null != operationLog)
                {
                    this.operationLogDAO.insertLogItem(operationLog);
                }

            }

            ICollection<NYBG_PM> list = this.GetSomeNYBG_PM(str[0], str[1],  1, 10000);

            int ph = 0;
            String phString = null;
            if (null != list)
            {
                foreach (NYBG_PM temp in list)
                {
                    phString = FormatPH(ph++);
                   
                    //如果当前文件名与新文件名一样则不做处理
                    if (temp.wjm.Equals(wjmStr + phString))
                    {
                        continue;
                    }
                    //如果当前文件名与新文件名不一致,则更新文件名及篇名信息
                    else
                    {
                        changeNYBG_PMWjm(temp.wjm, wjmStr + phString);
                    }
                }
            }
            return result;
        }





        /// <summary>
        /// 根据索引编码、年份、期号获取最大文件的篇名信息
        /// </summary>
        /// <param name="flbm">分类编码</param>
        /// <param name="nf">年份</param>
        /// <returns></returns>
        public NYBG_PM getMaxNYBG_PM(String flbm, String nf)
        {
            if (null == flbm || "".Equals(flbm))
            {
                return null;
            }
            if (null == nf || "".Equals(nf))
            {
                return null;
            }
            //设置匹配文件名
            String wjm = flbm + "-" + nf +"%";
            return sqlMapper.QueryForObject<NYBG_PM>("SelectMaxNYBG_PM", wjm);
        }

        /// <summary>
        /// 生成日志信息
        /// </summary>
        /// <param name="nybgPM">中文篇名信息</param>
        /// <param name="opType">操作类型</param>
        /// <param name="uname">用户名</param>
        /// <returns></returns>
        private OperationLogItem GetOperationLogItem(NYBG_PM nybgPM, String opType, String uname)
        {
            if (null == nybgPM)
            {
                return null;
            }

            OperationLogItem operationLogItem = new OperationLogItem();
            //操作类型
            operationLogItem.opType = opType;
            //数据库表
            operationLogItem.opTable = "NYBG_PM";
            //全文页数
            operationLogItem.opPageCount =nybgPM.ys;

            operationLogItem.opObjectID = "考察报告";
            // 操作涉及的书刊
            operationLogItem.opBookIn = "考察报告";

            // 时间
            operationLogItem.opDate = DateTime.Now;
            // 操作员
            operationLogItem.opPerson = uname;
            //记录操作模式，判断是否属于批量操作。取值范围SINGLE/BATCH。
            operationLogItem.opMode = "SINGLE";

            //操作的内容
            String content = "('" + nybgPM.flbm + "','" + nybgPM.sybm + "','" + nybgPM.wjm + "','" + nybgPM.ywjm + "','" + nybgPM.ys + "','" + nybgPM.bgmc + "','" + nybgPM.nf + "','" + nybgPM.cbrq + "','" + nybgPM.zz + "','" + nybgPM.zzdw + "','" + nybgPM.zbdw + "','" + nybgPM.ztc + "','" + nybgPM.wz + "','" + nybgPM.bgh + "','" + nybgPM.fltx +"','"+ nybgPM.zfl + "','" + nybgPM.wxbh + "','" + nybgPM.qw +"','"+ nybgPM.gxrq + "','" + nybgPM.dlr  + "')";
            operationLogItem.opContent = content;
            return operationLogItem;
        }


        /// <summary>
        /// 修改已有的文件名。
        /// </summary>
        /// <param name="oldWjm">旧文件名</param>
        /// <param name="newWjm">新文件名</param>
        /// <returns>是否修改成功</returns>
        public bool changeNYBG_PMWjm(String oldWjm, String newWjm)
        {
            if (null == oldWjm || null == newWjm)
            {
                return false;
            }

            IDictionary<String, Object> para = new Dictionary<String, Object>();
            para.Add("oldwjm", oldWjm);
            para.Add("newwjm", newWjm);

            bool result = true;
            try
            {
                sqlMapper.Update("ChangeNYBG_PM_wjm", para);
                //修改PDF文件名
                OperationFile.ReNameFile(oldWjm, newWjm);

            }
            catch
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 规范篇号
        /// </summary>
        /// <param name="ph">篇号</param>
        /// <returns>4位篇号</returns>
        private String FormatPH(int ph)
        {
            String phString = ph.ToString();
            //长度为1
            if (phString.Length == 1)
            {
                phString = "00" + phString;
            }
            //长度为2
            else if (phString.Length == 2)
            {
                phString = "0" + phString;
            }
            return phString + ".PDF";
        }

        public NYBG_DAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}